package com.SFA.OrderManagement;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.FrameWork.Common.LocalActivityManagerFragment;
import com.FrameWork.Entity.OrderEntity;
import com.FrameWork.Utils.DateTimeUtils;
import com.SFA.R;

public class OrderHeaderFragment extends LocalActivityManagerFragment implements OnTouchListener,OnClickListener {

	private OrderEntity entity = null;

	private TextView txtOrderDate, txtDeliveryDate, txtDeliveryType;
	private EditText txtRemark, txtContactTel, txtDeliveryAddress,
			txtContactName;
	private View layoutDate,layoutType;
	private TabHost mTabHost;
	private DatePicker calendar;
	private PopupWindow popupWindow;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.order_header, container, false);
		mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
		mTabHost.setup(getLocalActivityManager());

		TabSpec tab = mTabHost.newTabSpec("map").setIndicator("map")
				.setContent(new Intent(getActivity(), MapActivity.class));
		mTabHost.addTab(tab);

		txtOrderDate = (TextView) v.findViewById(R.id.txtOrderDate);
		txtDeliveryDate = (TextView) v.findViewById(R.id.txtDeliveryDate);
		txtDeliveryType = (TextView) v.findViewById(R.id.txtDeliveryType);
		txtRemark = (EditText) v.findViewById(R.id.txtRemark);
		txtContactTel = (EditText) v.findViewById(R.id.txtContactTel);
		txtDeliveryAddress = (EditText) v.findViewById(R.id.txtDeliveryAddress);
		txtContactName = (EditText) v.findViewById(R.id.txtContactName);
		layoutDate = v.findViewById(R.id.layoutDate);
		layoutDate.setOnTouchListener(this);
		layoutType = v.findViewById(R.id.layoutType);
		layoutType.setOnTouchListener(this);
		// 弹出式控制数字输入

		return v;

	}

	public void LoadData(OrderEntity entity) {
		this.entity = entity;
		if (entity.REQ_DELIVERY_DATE == null) {
			entity.REQ_DELIVERY_DATE = DateTimeUtils.GetCurrentTime();
		}

		txtOrderDate.setText(DateTimeUtils.getSelects12Date(entity.ORDER_DATE));
		txtContactName.setText(entity.CONTACT_MAN);
		txtContactTel.setText(entity.CONTACT_LINK);
		txtDeliveryAddress.setText(entity.DELIVERY_ADDRESS);
		txtRemark.setText(entity.ADDITIONAL_INFO);
		if (!TextUtils.isEmpty(entity.REQ_DELIVERY_DATE)) {
			txtDeliveryDate.setText(DateTimeUtils
					.getSelects12Date(entity.REQ_DELIVERY_DATE).substring(0, 11));
		}

		try {
			Resources resource = getActivity().getResources();
			String[] values = resource
					.getStringArray(R.array.STRING_ARRAY_DELIVERY_TYPE_VALUE);
			String[] titles = resource
					.getStringArray(R.array.STRING_ARRAY_DELIVERY_TYPE_TITLE);
			// 判断索引
			int i = 0;
			for (String str : values) {
				if (str.equals(String.valueOf(entity.DELIVERY_TYPE))) {
					this.txtDeliveryType.setText(titles[i]);
					break;
				}
				i++;
			}

		} catch (Exception err) {
			err.printStackTrace();
		}
	}

	public OrderEntity getEntity() {

		return entity;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() != MotionEvent.ACTION_UP)
			return false;
		if (v == layoutDate) {

			ShwoDate();
			
		} else if (v == layoutType) {

		}

		return false;
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btnCancel)
		{
			popupWindow.dismiss();
		}
		else
		{
			Calendar date = Calendar.getInstance();
			date.set(Calendar.YEAR, calendar.getYear());
			date.set(Calendar.MONTH, calendar.getMonth());
			date.set(Calendar.DAY_OF_MONTH, calendar.getDayOfMonth());
		
			entity.REQ_DELIVERY_DATE = DateTimeUtils.getLongDateFromMillis(date.getTimeInMillis());
			((TextView)getActivity().findViewById(R.id.txtDeliveryDate)).setText(DateTimeUtils
					.getSelects12Date(entity.REQ_DELIVERY_DATE).substring(0, 11));
			popupWindow.dismiss();
		}
		
	}
	
	private void ShwoDate() {

		final View popupView = LayoutInflater.from(getActivity()).inflate(
				R.layout.date_layout, null);
		popupView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				popupWindow.dismiss();
				return false;
			}
		});

		popupView.findViewById(R.id.main_layout).setOnTouchListener(
				new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						return true;
					}
				});


		popupView.findViewById(R.id.btnCancel).setOnClickListener(this);
		popupView.findViewById(R.id.btnEnter).setOnClickListener(this);
		calendar = (DatePicker) popupView.findViewById(R.id.calendarDate);
		
		Calendar max = Calendar.getInstance();
		max.add(Calendar.YEAR, 1);
		
		long maxDate = max.getTimeInMillis();
		calendar.setMaxDate(maxDate);
		Calendar today = Calendar.getInstance();
		Calendar min = new GregorianCalendar(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));

		
		calendar.setMinDate(min.getTimeInMillis());
		//calendar.setCalendarViewShown(true);
		//calendar.setHovered(true);
		int year,month,day;
		if(entity != null && TextUtils.isEmpty(entity.REQ_DELIVERY_DATE))
		{
			long time = DateTimeUtils.getMillisFromFromLongDate(entity.REQ_DELIVERY_DATE);
			today.setTimeInMillis(time);
		}
		year = today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH);
		day =today.get(Calendar.DAY_OF_MONTH);
		
		calendar.init(year, month, day, null);
		popupWindow = new PopupWindow(popupView,
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOutsideTouchable(true);
		popupWindow.setAnimationStyle(R.style.dialogWindowAnim);

		popupWindow.setTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.update();
		popupWindow.showAtLocation(getActivity().findViewById(R.id.main), Gravity.BOTTOM
				| Gravity.CENTER_HORIZONTAL, 0, 0);






	}
}
